-- UIQCAccountLogin
-- Created by chengb Sep/7/2016
-- 青瓷账号登录界面

UIQCAccountLogin = class("UIQCAccountLogin", function()
    return cc.Layer:create();
end)

function UIQCAccountLogin.create()
    return UIQCAccountLogin.new();
end

local MIN_ACCOUNT_LEN  = 6;   -- 账号最小长度
local MAX_ACCOUNT_LEN  = 32;  -- 账号最大长度
local MIN_PASSWORD_LEN = 6;   -- 密码最小长度
local MAX_PASSWORD_LEN = 16;  -- 密码最大长度

-- 构造函数
function UIQCAccountLogin:ctor()
    self:setName("UIQCAccountLogin");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/login/QCAccountLogin.csb");
    self:addChild(node);
    self.node = node;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIQCAccountLogin:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIQCAccountLogin:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "enter" then
            -- 显示登录界面的遮罩
            local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
            uiLogin:setMaskVisible(true);

        elseif ev == "exit" then
            -- 反注册所有事件
            EventMgr.removeAll("UIQCAccountLogin");

            -- 隐藏登录界面的遮罩
            local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
            if uiLogin then
                uiLogin:setMaskVisible(false);
            end
        end
    end);
end

-- 重绘
function UIQCAccountLogin:redraw()
    -- 标题
    local titleLabel = findChildByName(self.node, "CT/title");
    TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_BROWN);
    titleLabel:setString(getLocStr("account_login"));

    local CT = findChildByName(self.node, "CT");

    -- 输入账号
    local accountInput = findChildByName(CT, "account_node/input_account");
    local editBoxSize = accountInput:getContentSize();
    local editBox = ccui.EditBox:create(editBoxSize, "images/ui/login/1.png");
    editBox:setAnchorPoint(cc.p(0, 0.5));
    editBox:setPosition(accountInput:getPosition());
    editBox:setFontName(getLocFontName());
    editBox:setFontSize(TextStyleM.TEXT_SIZE_SMALL2);
    editBox:setPlaceHolder(getLocStr("input_account"));
    editBox:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_GRAY2);
    editBox:setFontColor(TextStyleM.TEXT_OUTLINE_COLOR_DARK);
    editBox:setMaxLength(MAX_ACCOUNT_LEN);
    editBox:setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE);
    editBox:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);
    self.editBoxAccount = editBox;
    accountInput:getParent():addChild(editBox);
    accountInput:setVisible(false);

    -- 尝试获取最近一次使用的用户名
    local lastUserName = UserDefaultM.getStringForKey("UserName");
    if type(lastUserName) == "string" and string.len(lastUserName) > 0 then
        self.editBoxAccount:setText(lastUserName);
    end

    -- 输入密码
    local pwdInput = findChildByName(CT, "pwd_node/input_pwd");
    local editBoxSize = pwdInput:getContentSize();
    local editBox = ccui.EditBox:create(editBoxSize, "images/ui/login/1.png");
    editBox:setAnchorPoint(cc.p(0, 0.5));
    editBox:setPosition(pwdInput:getPosition());
    editBox:setFontName(getLocFontName());
    editBox:setFontSize(TextStyleM.TEXT_SIZE_SMALL2);
    editBox:setPlaceHolder(getLocStr("input_password"));
    editBox:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_GRAY2);
    editBox:setFontColor(TextStyleM.TEXT_OUTLINE_COLOR_DARK);
    editBox:setMaxLength(MAX_PASSWORD_LEN);
    editBox:setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD);
    editBox:setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE);
    self.editBoxPwd = editBox;
    pwdInput:getParent():addChild(editBox);
    pwdInput:setVisible(false);

    -- 尝试获取最近一次使用的密码
    local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
    local lastPassword = uiLogin:getLastPassword();
    if type(lastPassword) == "string" and string.len(lastPassword) > 0 then
        self.editBoxPwd:setText(lastPassword);
    end

    -- 忘记密码
    local forgetPwdLabel = findChildByName(CT, "forget_pwd");
    TextStyleM.setTextStyle(forgetPwdLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_GRAY);
    forgetPwdLabel:setString(getLocStr("forget_password_tip"));
end

-- 注册按钮点击事件
function UIQCAccountLogin:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIQCAccountLogin");

            -- 显示上一级界面
            local uiChannelSelectQC = UIMgr.getCurrentScene():getFormByName("UIChannelSelectQC");
            uiChannelSelectQC:setVisible(true);
        end
    end
    AddTouchEventListener(btnBack, onBackClick);
    AddBackEventHandler(self, btnBack, btnBack.touchEventListener);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
    btnBack:setOpacity(0);

    -- 返回图标
    local backImg = findChildByName(self.node, "CT/back");
    backImg:setTouchEnabled(true);
    backImg:addTouchEventListener(onBackClick);

    -- 点击背景底图关闭
    local bgImg = findChildByName(self.node, "bg/bgImg");
    bgImg:setTouchEnabled(true);
    bgImg:addTouchEventListener(onBackClick);

    -- 注册登录按钮点击事件
    local btnLogin = findChildByName(self.node, "CT/login");
    local function onLoginClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:tryLogin();
        end
    end
    btnLogin:addTouchEventListener(onLoginClick);
    TextStyleM.setTextStyle(btnLogin, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_WHITE, false);
    btnLogin:setTitleText(getLocStr("btn_text_login"));

    -- 忘记密码
    local forgetPwdLabel = findChildByName(self.node, "CT/forget_pwd");
    forgetPwdLabel:setTouchEnabled(true);
    local function onForgetClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- TODO
        end
    end
end

-- 尝试登录
function UIQCAccountLogin:tryLogin()
    local account   = removeTabAndSpace(self.editBoxAccount:getText());
    local password  = removeTabAndSpace(self.editBoxPwd:getText());

    local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
    uiLogin.editName:setText(account);
    uiLogin.editPassword:setText(password);
    uiLogin:tryLogin();
end
